#!/user/bin/env set PYTHONIOENCODING=utf-8 && python -u
# -*- coding: utf-8 -*-

from .primitives import Primitives

class Collections:

    @staticmethod
    def is_empty(collection):
        return collection is None or (Primitives.is_collection(collection) and len(collection) == 0)

    @staticmethod
    def is_not_empty(collection):
        return not Collections.is_empty(collection)

    @staticmethod
    def first(collection, condition=lambda i, e:True):
        if Collections.is_not_empty(collection):
            for index, element in enumerate(collection):
                if condition(index, element):
                    return element
        return None

    @staticmethod
    def last(collection, condition=lambda i, e:True):
        if Collections.is_not_empty(collection):
            for index in range(len(collection) - 1, -1, -1):
                if condition(index, collection[index]):
                    return collection[index]
        return None
